package cn.iocoder.yudao.module.acm.controller.admin.textileproduceplan;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.acm.controller.admin.textileproduceplan.vo.TextileProducePlanPageReqVO;
import cn.iocoder.yudao.module.acm.controller.admin.textileproduceplan.vo.TextileProducePlanRespVO;
import cn.iocoder.yudao.module.acm.controller.admin.textileproduceplan.vo.TextileProducePlanSaveReqVO;
import cn.iocoder.yudao.module.acm.controller.admin.textileproduceplan.vo.TextileProducePlanSimpleRespVO;
import cn.iocoder.yudao.module.acm.dal.dataobject.textileproduceplan.TextileProducePlanDO;
import cn.iocoder.yudao.module.acm.service.textileproduceplan.TextileProducePlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.pojo.PageParam.PAGE_SIZE_NONE;

@Tag(name = "管理后台 - 航机管理-纺织品生产计划")
@RestController
@RequestMapping("/pplan/textile-produce-plan")
@Validated
public class TextileProducePlanController {

    @Resource
    private TextileProducePlanService textileProducePlanService;

    @PostMapping("/create")
    @Operation(summary = "创建航机管理-纺织品生产计划")
    @PreAuthorize("@ss.hasPermission('acm:textile-produce-plan:create')")
    public CommonResult<Long> createTextileProducePlan(@Valid @RequestBody TextileProducePlanSaveReqVO createReqVO) {
        return success(textileProducePlanService.createTextileProducePlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新航机管理-纺织品生产计划")
    @PreAuthorize("@ss.hasPermission('acm:textile-produce-plan:update')")
    public CommonResult<Boolean> updateTextileProducePlan(@Valid @RequestBody TextileProducePlanSaveReqVO updateReqVO) {
        textileProducePlanService.updateTextileProducePlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除航机管理-纺织品生产计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('acm:textile-produce-plan:delete')")
    public CommonResult<Boolean> deleteTextileProducePlan(@RequestParam("id") Long id) {
        textileProducePlanService.deleteTextileProducePlan(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除航机管理-纺织品生产计划")
                @PreAuthorize("@ss.hasPermission('acm:textile-produce-plan:delete')")
    public CommonResult<Boolean> deleteTextileProducePlanList(@RequestParam("ids") List<Long> ids) {
        textileProducePlanService.deleteTextileProducePlanListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得航机管理-纺织品生产计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('acm:textile-produce-plan:query')")
    public CommonResult<TextileProducePlanRespVO> getTextileProducePlan(@RequestParam("id") Long id) {
        TextileProducePlanRespVO respVO = textileProducePlanService.getTextileProducePlan(id);
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得航机管理-纺织品生产计划分页")
    @PreAuthorize("@ss.hasPermission('acm:textile-produce-plan:query')")
    public CommonResult<PageResult<TextileProducePlanRespVO>> getTextileProducePlanPage(@Valid TextileProducePlanPageReqVO pageReqVO) {
        PageResult<TextileProducePlanDO> pageResult = textileProducePlanService.getTextileProducePlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TextileProducePlanRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得航机管理-纺织品生产计划分页")
    @PreAuthorize("@ss.hasPermission('acm:textile-produce-plan:query')")
    public CommonResult<PageResult<TextileProducePlanSimpleRespVO>> getTextileProducePlanSimplePage(@Valid TextileProducePlanPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PAGE_SIZE_NONE); // 不分页
        PageResult<TextileProducePlanDO> pageResult = textileProducePlanService.getTextileProducePlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TextileProducePlanSimpleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出航机管理-纺织品生产计划 Excel")
    @PreAuthorize("@ss.hasPermission('acm:textile-produce-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTextileProducePlanExcel(@Valid TextileProducePlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TextileProducePlanDO> list = textileProducePlanService.getTextileProducePlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "航机管理-纺织品生产计划.xls", "数据", TextileProducePlanRespVO.class,
                        BeanUtils.toBean(list, TextileProducePlanRespVO.class));
    }

}